import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_search.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/views/public_refresh.dart';
import 'package:honeypotflutter/public/public_total.dart';
import '../api/preferencesApi.dart';
import '../model/preferences_listModel.dart';
import '../view/preferences_listItem.dart';

class PreferencesSearchPage extends StatefulWidget {
  const PreferencesSearchPage({Key? key}) : super(key: key);

  @override
  State<PreferencesSearchPage> createState() => _PreferencesSearchPageState();
}

class _PreferencesSearchPageState extends State<PreferencesSearchPage> {
  /// 刷新
  late HLRefreshController _refreshController = HLRefreshController(initialRefresh: false);

  /// 使用户号
  String? _phone;

  /// 是否删除
  bool _isDelete = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(
        navigationTitle: "收件人偏好设置",
        backAction: () {
          Get.back<bool>(result: _isDelete);
        },
        bottom: PreferredSize(
            preferredSize: Size(double.infinity, 50),
            child: MGSearch(
              hintText: "请输入手机号码查询",
              onClick: (value) {
                if (!GetUtils.isPhoneNumber(value)) {
                  HLTotal.total("请输入正确手机号码");
                  return;
                }
                _phone = value;
                _refreshController.requestRefresh();
              },
            )),
      ),
      body: HLRefresh<PreferencesListModel>(
          tag: "202020",
          refreshController: _refreshController,
          isLoading: false,
          onRefresh: () async {
            return await PreferencesApi.requestList(page: 1, phone: _phone);
          },
          snap: (PreferencesListModel model, int index) {
            return PreferencesListItem(
              itemModel: model,
              deleteAction: () {
                _refreshController.requestRefresh();
                _isDelete = true;
              },
            );
          }),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
